﻿Imports System.Data.OleDb

Public Class RecipeDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'The below code serves to load the data in the comments gridview via code behind
        If IsPostBack = False Then
            If Request.Params("ID") = "" Then Response.Redirect("RecipeList.aspx")
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM COMMENT WHERE REC_ID=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            oleDbConn.Open()

            Dim dataReader = cmd.ExecuteReader()
            GridView2.DataSource = dataReader
            GridView2.DataBind()
        End If
    End Sub

    Protected Sub btn_AddComment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_AddComment.Click

        Dim d As DateTime = Now
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("recipeConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO COMMENT(USER_ID,COMM_DESC,COMM_DATE,REC_ID) VALUES (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_CommComment.Text)
        cmd.Parameters.AddWithValue("@f3", d.ToString("yyyy-MM-dd"))
        cmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'The below code is to show the comment immediately in gridview 2
        Dim fillComments As String = "SELECT * FROM COMMENT WHERE REC_ID=@f1"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.CommandType = CommandType.Text
        fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dataReader = fillCommentsCmd.ExecuteReader()
        GridView2.DataSource = dataReader
        GridView2.DataBind()

        'The below code is used to clear the textfields after the user has submitted a comment
        tb_CommComment.Text = ""
    End Sub
End Class